package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.SalRep;

public class SalRepDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public SalRepDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}

	@SuppressWarnings("static-access")
	public int createOrUpdateSalRep(ArrayList<SalRep>  list) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
		cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_FSALREP, null);
		if(cursor.getCount()>0){
			int success = dB.delete(dbHelper.TABLE_FSALREP, null, null);
			Log.v("Success", success+"");
		}
			
			
    for (SalRep rep : list) {
		
			ContentValues values = new ContentValues();
			
			values.put(dbHelper.FSALREP_ASE_CODE, rep.getFSALREP_ASE_CODE());
			values.put(dbHelper.FSALREP_AREA_CODE, rep.getFSALREP_AREA_CODE());
			values.put(dbHelper.FSALREP_DEAL_CODE, rep.getFSALREP_DEAL_CODE());
			values.put(dbHelper.FSALREP_RECORD_ID, rep.getFSALREP_RECORD_ID());
			values.put(dbHelper.FSALREP_REP_CODE, rep.getFSALREP_REP_CODE());
			values.put(dbHelper.FSALREP_REP_PREFIX, rep.getFSALREP_REP_PREFIX());
			values.put(dbHelper.FSALREP_REP_TCODE, rep.getFSALREP_REP_TCODE());
			values.put(dbHelper.FSALREP_REP_PHONE_NO, rep.getFSALREP_REP_PHONE_NO());
			values.put(dbHelper.FSALREP_REP_NAME, rep.getFSALREP_REP_NAME());
			values.put(dbHelper.FSALREP_REP_EMAIL, rep.getFSALREP_REP_EMAIL());
			values.put(dbHelper.FSALREP_REP_MOB, rep.getFSALREP_REP_MOB());
			values.put(dbHelper.FSALREP_PASSWORD, rep.getFSALREP_PASSWORD());
			
			count = (int) dB.insert(dbHelper.TABLE_FSALREP, null, values);

			}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	@SuppressWarnings("static-access")
	public String getCurrentRepCode(){
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FSALREP ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FSALREP_REP_CODE));
			
		}
			
		return "";
	}
	
	
	public ArrayList<SalRep> getSaleRepDetails(){
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<SalRep> rep_list = new ArrayList<SalRep>();
		
		String selectRep = "SELECT * FROM " + dbHelper.TABLE_FSALREP ;
		
		Cursor curRep = null;
		curRep = dB.rawQuery(selectRep, null);
		
		while(curRep.moveToNext()){
			
			SalRep newRep = new SalRep();
			newRep.setFSALREP_ID(curRep.getString(0));
			newRep.setFSALREP_ASE_CODE(curRep.getString(1));
			newRep.setFSALREP_AREA_CODE(curRep.getString(2));
			newRep.setFSALREP_DEAL_CODE(curRep.getString(3));
			newRep.setFSALREP_RECORD_ID(curRep.getString(4));
			newRep.setFSALREP_REP_CODE(curRep.getString(5));
			newRep.setFSALREP_REP_PREFIX(curRep.getString(6));
			newRep.setFSALREP_REP_TCODE(curRep.getString(7));
			newRep.setFSALREP_REP_PHONE_NO(curRep.getString(8));
			newRep.setFSALREP_REP_NAME(curRep.getString(9));
			newRep.setFSALREP_REP_EMAIL(curRep.getString(10));
			newRep.setFSALREP_REP_MOB(curRep.getString(11));
			newRep.setFSALREP_PASSWORD(curRep.getString(12));
			rep_list.add(newRep);
			
		}
		
		return rep_list;
	}
	
	@SuppressWarnings("static-access")
	public String getDealCode(){
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FSALREP ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FSALREP_DEAL_CODE));
			
		}
			
		return "";
	}
	
	
	
	public String getAreaCode(){
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FSALREP ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FSALREP_AREA_CODE));
			
		}
			
		return "";
	}
	
	
	@SuppressWarnings("static-access")
	public String getCurrentDealerCode() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
				
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FSALREP;
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FSALREP_DEAL_CODE));
			
		}
		
		return "";
	}
}
